<?php
class Hasoffer extends CI_Model
{
	function PostReport($arr)
	{
		//print_r($arr);
		$this->load->model('common');
		$rs=$this->common->GetById('api',array('id'=>$arr['api']));
		
		$target="Affiliate_Report.json";
		$method="Method=getStats";
		//print_r($rs);
		if($arr['api']==-1)
		{
			
			$apiKey="api_key=".apiKey;
		}
		else
			$apiKey="api_key=".$rs[0]->api_key;
		if($arr['api']==-1)
			$networkId="NetworkId=".networdID;
		else
			$networkId="NetworkId=".$rs[0]->name;
		$current=0;
		$perpage=10000;
		//$fields='&page=1&limit=10000&fields[]=Stat.offer_id&fields[]=Country.name&fields[]=Browser.display_name&fields[]=Stat.datetime&fields[]=Offer.name&fields[]=Country.name&fields[]=Browser.display_name&fields[]=Stat.conversion_status&fields[]=Stat.payout&fields[]=Stat.ip&fields[]=ConversionsMobile.device_brand&fields[]=ConversionsMobile.device_model&fields[]=ConversionsMobile.device_os&fields[]=ConversionsMobile.device_os_version&sort[Stat.datetime]=desc&filters[Stat.date][conditional]=BETWEEN&filters[Stat.date][values][]='.$data_start.'&filters[Stat.date][values][]='.$data_end.'&data_start='.$data_start.'&data_end='.$data_end.'&hour_offset=-10&fields[]=Stat.session_ip&fields[]=Stat.ip&fields[]=Stat.user_agent&fields[]=Stat.refer';'
		$page='page='.$current;
		$limit='limit='.$perpage;
		
		
		$fields='';// '&type=daily';//&fields[]=Stat.offer_name';
		$fields.='&fields[]=Stat.date';
		if($arr['type']==1){$fields.='&fields[]=Stat.hour';}
		$fields.='&fields[]=Stat.offer_id';
		$fields.='&fields[]=Stat.conversions';
		$fields.='&fields[]=Stat.impressions';
		$fields.='&fields[]=Stat.clicks';
		$fields.='&fields[]=Stat.unique_clicks';
		$fields.='&fields[]=Offer.name';
		$fields.='&fields[]=Country.name';
		$fields.='&fields[]=Browser.display_name';

		
		if($arr['type']==1)
		{
			$fields.='&groups[]=Stat.date';
			$fields.='&groups[]=Stat.hour';
			$fields.='&groups[]=Stat.month';	
		}
		if($arr['type']==2)
		{
			$fields.='&groups[]=Stat.date';
			$fields.='&groups[]=Stat.month';	
		}
		
		if($arr['type']==3)
		{
			$fields.='&groups[]=Stat.month';	
		}
		$fields.='&groups[]=Country.name';
		$fields.='&groups[]=Stat.offer_id';
		
		if($arr['offer']!=-1)
		{
			$fields.='&filters[Stat.offer_id][conditional]=EQUAL_TO';
			$fields.='&filters[Stat.offer_id][values][]='.$arr['offer'];
		}
		//$fields.='&filters[Stat.date][conditional]=BETWEEN';
		//$fields.='&filters[Stat.date][values][]='."2014-10-1";//.$data_start;
		//$fields.='&filters[Stat.date][values][]='."2014-10-10";//.$data_end;
		$fields.='&data_start='.$arr['from'].'&data_end='.$arr['to'];
		
		$url=hasofferBase.$target."?".$method."&".$apiKey."&".$networkId."&".$page."&".$limit;
		$url=$url."&".$fields;
		//print_r($url);
		$rs=$this->curl->simple_get($url);
		//echo $url;
		//print_r($rs);
		return $rs; 
	}
	function GetApi()
	{
	    $user=$this->session->userdata('user_info');
	    //print_r($user);
	    if($user->username!='root')
	    {
	        $this->db->select('api.id,api.name');
	        $this->db->from('api');
	        $this->db->join('users_api','api.id=users_api.api_id');
	        
	       $this->db->where('users_api.user_id',$user->id);
	       
	       $rs=$this->db->get();
	       return $rs->result();
	    }
	    else
	    {
	       $rs=$this->common->Get('api');
	       return $rs;
	    }
	    
	    
	}
	function UserGetApi()
	{
	    $user=$this->session->userdata('user_info');
	    //print_r($user);
	    if($user->username!='root')
	    {
	        $this->db->select('api.id,api.name');
	        $this->db->from('api');
	        $this->db->join('users_api','api.id=users_api.api_id','left');
	         
	        $this->db->where("users_api.user_id='".$user->id."' OR users_api.user_id=NULL");
	
	        $rs=$this->db->get();
	        return $rs->result();
	    }
	    else
	    {
	        $rs=$this->common->Get('api');
	        return $rs;
	    }
	     
	     
	}
	function GetOffer($arr=array())
	{
		//print_r($arr);
		$this->load->model('common');
		$rs=$this->common->GetById('api',array('id'=>$arr['api']));
		
		$target="Affiliate_Offer.json";
		$method="Method=findMyOffers";
		if($arr['api']==-1)
		{
			
			$apiKey="api_key=".apiKey;
		}
		else
			$apiKey="api_key=".$rs[0]->api_key;
		if($arr['api']==-1)
			$networkId="NetworkId=".networdID;
		else
			$networkId="NetworkId=".$rs[0]->name;
		//$fields='&fields[]=id&fields[]=name';
		$url=hasofferBase.$target."?".$method."&".$apiKey."&".$networkId;
		$rs=$this->curl->simple_get($url);
		
		return $rs;
	}
} 